package com.lgj.core;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lgj.result.ResultBody;
import lombok.Synchronized;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: guangjie.liao
 * @Date: 2022/12/6 10:50
 * @Description: token 认证失败异常处理
 */
public class OauthResourceAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setContentType("application/json");
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(response.getOutputStream(), ResultBody.fail(authException.getMessage()));
    }
}
